package cn.iocoder.yudao.module.blog.controller.admin.postlike;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.postlike.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.postlike.BlogPostLikeDO;
import cn.iocoder.yudao.module.blog.convert.postlike.BlogPostLikeConvert;
import cn.iocoder.yudao.module.blog.service.postlike.BlogPostLikeService;

@Tag(name = "管理后台 - 帖子点赞")
@RestController
@RequestMapping("/blog/post-like")
@Validated
public class BlogPostLikeController {

    @Resource
    private BlogPostLikeService postLikeService;

    @PostMapping("/create")
    @Operation(summary = "创建帖子点赞")
    @PreAuthorize("@ss.hasPermission('blog:post-like:create')")
    public CommonResult<Long> createPostLike(@Valid @RequestBody BlogPostLikeCreateReqVO createReqVO) {
        return success(postLikeService.createPostLike(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新帖子点赞")
    @PreAuthorize("@ss.hasPermission('blog:post-like:update')")
    public CommonResult<Boolean> updatePostLike(@Valid @RequestBody BlogPostLikeUpdateReqVO updateReqVO) {
        postLikeService.updatePostLike(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除帖子点赞")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:post-like:delete')")
    public CommonResult<Boolean> deletePostLike(@RequestParam("id") Long id) {
        postLikeService.deletePostLike(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得帖子点赞")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:post-like:query')")
    public CommonResult<BlogPostLikeRespVO> getPostLike(@RequestParam("id") Long id) {
        BlogPostLikeDO postLike = postLikeService.getPostLike(id);
        return success(BlogPostLikeConvert.INSTANCE.convert(postLike));
    }

    @GetMapping("/list")
    @Operation(summary = "获得帖子点赞列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:post-like:query')")
    public CommonResult<List<BlogPostLikeRespVO>> getPostLikeList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogPostLikeDO> list = postLikeService.getPostLikeList(ids);
        return success(BlogPostLikeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得帖子点赞分页")
    @PreAuthorize("@ss.hasPermission('blog:post-like:query')")
    public CommonResult<PageResult<BlogPostLikeRespVO>> getPostLikePage(@Valid BlogPostLikePageReqVO pageVO) {
        PageResult<BlogPostLikeDO> pageResult = postLikeService.getPostLikePage(pageVO);
        return success(BlogPostLikeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出帖子点赞 Excel")
    @PreAuthorize("@ss.hasPermission('blog:post-like:export')")
    @OperateLog(type = EXPORT)
    public void exportPostLikeExcel(@Valid BlogPostLikeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogPostLikeDO> list = postLikeService.getPostLikeList(exportReqVO);
        // 导出 Excel
        List<BlogPostLikeExcelVO> datas = BlogPostLikeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "帖子点赞.xls", "数据", BlogPostLikeExcelVO.class, datas);
    }

}
